namespace SimpleWebServices.Models
{
    using System;
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Security.Cryptography;
    using Xunit;

    public sealed class FormSessionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<FormSession>()
                .DerivesFrom<SimpleWebServices.Data.Record>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("session")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            FormSession actual = (
                "<session>" +
                    "<data>" +
                        "<value name='foo'>bar</value>" +
                    "</data>" +
                "</session>").Deserialize<FormSession>();

            Assert.NotNull(actual);

            Assert.Equal<string>("bar", actual.Data["foo"]);
        }

        [Fact]
        public void deserializeEmpty()
        {
            FormSession actual = "<session />".Deserialize<FormSession>();

            Assert.NotNull(actual);

            Assert.Empty(actual.Data);
        }

        [Fact]
        public void serialize()
        {
            FormSession obj = new FormSession
            {
                Data = new DataCollection
                {
                    { "foo", "bar" }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/session/data/value[@name='foo'][text()='bar'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new FormSession());
        }

        [Fact]
        public void prop_Data()
        {
            Assert.True(new PropertyExpectations(typeof(FormSession).GetProperty("Data"))
                .XmlElement("data")
                .TypeIs<DataCollection>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_Entity()
        {
            Assert.True(new PropertyExpectations(typeof(FormSession).GetProperty("Entity"))
                .XmlIgnore()
                .TypeIs<string>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_Entity_get()
        {
            FormSession obj = new FormSession
            {
                Data = new DataCollection
                {
                    { "foo", "bar" }
                }
            };

            string expected = "6YLZppRxOLl/X91qPdRvZg==";
            string actual = MD5Hash.Compute(obj.Entity);

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void prop_Entity_getEmpty()
        {
            string expected = string.Empty;
            string actual = new FormSession().Entity;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_FormatUrn_Token()
        {
            Token token = Token.New();

            AbsoluteUri expected = "urn://session/" + token;
            AbsoluteUri actual = FormSession.FormatUrn(token);

            Assert.Equal<AbsoluteUri>(expected, actual);
        }

        [Fact]
        public void op_FormatUrn_TokenEmpty()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => FormSession.FormatUrn(Token.Empty));
        }

        [Fact]
        public void op_Store()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository sessions = FakeRepository.Register<FormSession>();

                (new FormSession
                {
                    Urn = "urn://session/" + Token.New(),
                    Data = new DataCollection
                    {
                        { "foo", "bar" }
                    }
                }).Store();

                Assert.True((bool)sessions.Xml.CreateNavigator().Evaluate("1=count(/repository/object[@token][@urn]/session/data/value[@name='foo'][text()='bar'])"));
            }
            finally
            {
                FakeRepository.Reset();
            }
        }
    }
}